package org.obeonetwork.dsl.spem.extension.ifest.actions;


import ifest.ontology.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Vector;


import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.obeonetwork.dsl.spem.BreakdownElement;
import org.obeonetwork.dsl.spem.TaskUse;
import org.obeonetwork.dsl.spem.TaskDefinition;
import org.obeonetwork.dsl.spem.extension.spem_ifest.EngineeringDomain;
import org.obeonetwork.dsl.spem.extension.spem_ifest.MetaModel;
import org.obeonetwork.dsl.spem.impl.ActivityImpl;
import org.obeonetwork.dsl.spem.impl.TaskDefinitionImpl;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;


import fr.obeo.dsl.viewpoint.DDiagramElementContainer;
import fr.obeo.dsl.viewpoint.impl.ViewpointFactoryImpl;
import fr.obeo.dsl.viewpoint.tools.api.ui.IExternalJavaAction;
import fr.obeo.dsl.viewpoint.util.ViewpointAdapterFactory;
/*
 * example code
 * */
public class GenerateToolAdapterAction implements IExternalJavaAction{

	private ActivityImpl element = null;
	public boolean canExecute(Collection<? extends EObject> arg0) {
		// TODO Auto-generated method stub
		if (arg0.size() != 1) return false;
		EObject o = arg0.iterator().next();		
		if (! (o instanceof ActivityImpl)) return false;
		element = (ActivityImpl)o;
	
		return true;
	}

	public void execute(Collection<? extends EObject> selections,
			Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		GenerateToolAdapter action = new GenerateToolAdapter();
		String rs = action.test();
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		MessageBox messBox = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		messBox.setMessage(rs);
		messBox.open();
		
		
		
		
	}
private class GenerateToolAdapter {
	private List<TaskDefinition> tasks =  new ArrayList<TaskDefinition>();
	private List<EngineeringDomain> contexts = new ArrayList<EngineeringDomain>();
	private List<List<Vector<MetaModel>>> transformations = new ArrayList<List<Vector<MetaModel>>>();
	public void run(ActivityImpl activity)
	{
		
		//get tasks
		List<BreakdownElement> bdElements = activity.getNestedBreakdownElement();
		for (BreakdownElement e:bdElements)
		{
			if (e instanceof TaskUse)
			{
				TaskUse taskuse = (TaskUse)e;
				tasks.add(taskuse.getTask());
			}
		}/*
		for(TaskDefinition e:tasks)
		{
			org.obeonetwork.dsl.spem.extension.spem_ifest.TaskDefinition task = (org.obeonetwork.dsl.spem.extension.spem_ifest.TaskDefinition) e;
			//get context
			EngineeringDomain context = (EngineeringDomain) task.getSupport().eContainer();
			contexts.add(context);
			//get essential transformation
			org.obeonetwork.dsl.spem.extension.spem_ifest.Tool ifesttool = (org.obeonetwork.dsl.spem.extension.spem_ifest.Tool) (task.getUsedTool().get(0));
//			ifesttool.getIsTypeOf()
		}*/
		//identify the context (domain of Task)
		//get metamodel transform need
	}
	public String test()
	{
		//String domain = "http://www.obeonetwork.org/ontologies/spem/ifest.owl#EngineeringDomain_DandI" ;
		ReasonerTest test = new ReasonerTest();
		String rs;
		try {
			rs = test.run();
			return rs;
		} catch (OWLOntologyCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	
		
	}
}
}
